var express = require('express');
var usersRouter = express.Router();
let jwt = require('../utils/JWT.js');
const SQLQuery = require('../utils/query.js');

usersRouter.post('/mini/getOpenId', async function (req, res) {
    try {
        let queryObj = req.body;
        let session = await this.common.jscode2session(queryObj.code);
        let sql = `select * from wx_account WHERE openId = '${session.openid}'`;
        let addSql = 'insert into wx_account(openId,sessionKey,createTime) values(?,?,?)';
        var addSqlParams = [session.openid, session.session_key, new Date()];
        let accountData = await SQLQuery.query(sql);

        if (accountData.length == 0) {
            let result = await SQLQuery.query(addSql, addSqlParams);
            const token = jwt.generate({
                id: result.insertId,
                username: session.openid
            }, "1h")
            res.header("Authorization", token)
            res.send(global.common.resJson(0, 'success', accountData[0]));
        } else {
            const token = jwt.generate({
                id: accountData[0].uid,
                username: accountData[0].openId
            }, "1h")
            res.header("Authorization", token)
            res.send(global.common.resJson(0, 'success', accountData[0]));
        }
    } catch (err) {
        res.send(err);
    }

})

usersRouter.post('/mini/saveUserheader', async function (req, res) {
    try {
        let queryObj = req.body;
        let sql = "update wx_account set profilePhoto = ? where uid = ?";
        let updateSqlParams = [queryObj.AvatarUrl, queryObj.uid];
        let result = await SQLQuery.query(sql, updateSqlParams);
        res.send(global.common.resJson(0, result.affectedRows === 1 ? '修改成功' : '无修改内容', null))
    } catch (err) {
        res.send(err)
    }

})

usersRouter.post('/mini/saveNickname', async function (req, res) {
    try {
        let queryObj = req.body;
        let sql = "update wx_account set nickname = ? where uid = ?";
        let updateSqlParams = [queryObj.nickname, queryObj.uid];
        let result = await SQLQuery.query(sql, updateSqlParams);
        res.send(global.common.resJson(0, result.affectedRows === 1 ? '修改成功' : '无修改内容', null))
    } catch (err) {
        res.send(err)
    }
})


usersRouter.post('/mini/getUserinfo', async function (req, res) {
    try {
        let queryObj = req.body;
        let sql = `SELECT * FROM wx_account WHERE uid = ${queryObj.uid}`;
        let result = await SQLQuery.query(sql);
        if (result.length == 1) {
            res.send(global.common.resJson(0, 'success', result[0]))
        } else {
            res.send(global.common.resJson(0, '无用户信息', null))
        }

    } catch (err) {
        res.send(err)
    }
})

usersRouter.post('/mini/setUserinfo', async function (req, res) {
    try {
        let queryObj = req.body;
        let sql = `update wx_account set nickname = ?, gender = ?,phone=? where uid = ?`;
        let sqlParams = [queryObj.nickname, queryObj.gender, queryObj.phone, queryObj.uid];
        let result = await SQLQuery.query(sql, sqlParams);

        res.send(global.common.resJson(0, 'success', null))

    } catch (err) {
        res.send(err)
    }
})


usersRouter.post('/mini/test', async function (req, res) {
    try {
        let result = await global.common.getApiQuota();
        res.send(result);
    } catch (err) {
        res.send(err);
    }

})




module.exports = usersRouter;
